package com.dc.ftp.activity.loan;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.dc.ftp.R;
import com.dc.ftp.adapter.SelectPlatformLvAdapter;
import com.dc.ftp.base.SPBaseActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Handler;

public class SelectLoanPlatformActivity extends SPBaseActivity implements View.OnClickListener {

    private ListView mListView;
    private SwipeRefreshLayout swipeRefreshLayout;
    private List<Map<String,Object>> mList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_loan_platform);
        super.init();
        setHeadTitle(true,"选择贷款平台");
    }

    @Override
    public void initView() {
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
        swipeRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.color_tab_item_fous));
        mListView = (ListView) findViewById(R.id.lv_loanPlatform);


    }

    @Override
    public void initData() {
        mList = new ArrayList<>();
        HashMap<String, Object>   map1 = new HashMap<>();
            map1.put("plat_name","宜人贷");
            map1.put("loan_max","5万元");
            map1.put("loan_rate","12%");

        HashMap<String, Object>   map2 = new HashMap<>();
        map2.put("plat_name","百度贷款");
        map2.put("loan_max","10万元");
        map2.put("loan_rate","10%");

        HashMap<String, Object>   map3 = new HashMap<>();
        map3.put("plat_name","微信贷款");
        map3.put("loan_max","3万元");
        map3.put("loan_rate","8%");

        HashMap<String, Object>   map4 = new HashMap<>();
        map4.put("plat_name","我来贷");
        map4.put("loan_max","1万元");
        map4.put("loan_rate","6%");

        mList.add(map1);
        mList.add(map2);
        mList.add(map3);
        mList.add(map4);

        SelectPlatformLvAdapter lvAdapter = new SelectPlatformLvAdapter(this, mList);
        mListView.setAdapter(lvAdapter);

    }

    @Override
    public void initEvent() {
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener(){

            @Override
            public void onRefresh() {
               swipeRefreshLayout.setRefreshing(true);
                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                swipeRefreshLayout.setRefreshing(false);
                                mListView.deferNotifyDataSetChanged();
                            }
                        });

                    }
                },3000);

            }
        });

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public ImageView checkView;

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                checkView = (ImageView) view.findViewById(R.id.iv_check_platform);
                checkView.setVisibility(View.VISIBLE);
                Intent intent = new Intent();
                Map<String, Object> map = mList.get(position);
                intent.putExtra("plat_name",map.get("plat_name").toString());
                setResult(333,intent);
                finish();

            }
        });

    }

    @Override
    public void onClick(View v) {

    }
}
